/**
 *
 */

var app = getApp()
Page({
	data: {
		root: app.setting.att_url,
		banner: [],
		goods: [],
		page:1,
		pagedata:[],
		pagetitle:'我的钱包',
		bannerHeight: Math.ceil(290.0 / 750.0 * getApp().screenWidth)
	},
	onPullDownRefresh: function (){
		var the=this;
		var opt={};
		the.onLoad(opt);
		setTimeout(function(){
			wx.stopPullDownRefresh()
		},1000);
	},
	onLoad: function (options){
		var the = this
		wx.setNavigationBarTitle({'title':the.data.pagetitle});
		var type = options.type
		app.util.get(app.access,function(pagedata){
			//console.log(pagedata);
			the.setData({
				pagedata:pagedata,
				loading:false
			});
			the.getlist(1);
		});
	},
	//翻页
	onReachBottom: function () {
		var isnodata=this.data.isnodata
		var loading=this.data.loading
		if(!isnodata && !loading){
			this.getlist()
		}
	},
	getlist: function(e){
		var the = this
		var isnodata = the.data.isnodata;
		var page = the.data.page;
		var pagedata = the.data.pagedata;
		if(e){
			if(e.currentTarget){
				var data=e.currentTarget.dataset;
				if(data.type){
					pagedata.params.type=data.type;
				}
			}
			page = 1;
			pagedata.list=[];
			isnodata=false;
		}else{
			if(isnodata){
				return false;
			}
		}
		var query='page='+page;
		var more_query=app.util.made_query(pagedata.params);
		if(more_query){
			query+='&'+more_query;
		}
		app.util.get('core/member.credit_log/list/'+query,function(res){
			//console.log('2121');console.log(res);
			//加载分页
			if(res.list.length>0){
					pagedata.list = pagedata.list.concat(res.list);
					if(page >1){
						$(".goods-item").animate({scrollTop:0},300);
					}
					page++;
			}else{
					isnodata = true
			}
			the.setData({
				'pagedata.list':pagedata.list,
				'pagedata.params':pagedata.params,
				page:page,
				isnodata:isnodata,
				loading:false
			});
		});
	},
	showpassword:function(e){
		var the = this
		var pagedata = the.data.pagedata;
		pagedata.showpassword=pagedata.showpassword?false:true
		the.setData({
			'pagedata.showpassword':pagedata.showpassword,
		});
	},
	showzhuan:function(e){
		var the = this
		var pagedata = the.data.pagedata;
		pagedata.showzhuan=pagedata.showzhuan?false:true
		the.setData({
			'pagedata.showzhuan':pagedata.showzhuan,
		});
	},
	mobile_login:function(e){
		var the=this
		var pagedata=the.data.pagedata
		//根据openid为用户登陆，此处尝试获取用户手机号
		app.util.post('core/member.set/setmobile/',e.detail,function(res){
			//console.log(res);
			pagedata.item.mobile=res.mobile
			the.setData({
				'pagedata.item':pagedata.item
			})
		});
	},
	uppassword: function (e) {
		var the = this
		this.setData({
		  'pagedata.post.password': e.detail.value
		})
	},
	sms_code: function (e) {
		var the = this
		tool.mobile_code({'mobile':pagedata.member.mobile,'oldmobile':pagedata.member.oldmobile},'sms_code')
		this.setData({
		  'pagedata.post.password': e.detail.value
		})
	},
	showrecharge:function(e){
		var the = this
		var pagedata = the.data.pagedata;
		pagedata.showrecharge=pagedata.showrecharge?false:true
		the.setData({
			'pagedata.showrecharge':pagedata.showrecharge,
		});
	},
	click:function(evt){
		//用户点击事件 data.func 为方法名，data.params 为调用参数！
		var data=evt.currentTarget.dataset;
		//console.log(evt);
		app.util[data.func](data.params);
	},
	upfee:function(e){
		var the = this
		var data=e.detail.value;
		var pagedata = the.data.pagedata;
		pagedata.post.fee=parseFloat(data/pagedata.jifen_rate);
		the.setData({
			'pagedata.post.jifen':e.detail.value,
			'pagedata.post.fee':pagedata.post.fee,
		});
	},
	closepaymodal:function(){
		var page=this
		page.setData({
			showpaymodal:false
		});
	},
	pay_credit:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_credit(pagedata.paymodal.id,function(res){
				page.setData({
					showpaymodal:false,
				});
				app.util.msg(res.message,'','core/member.xunibi//type='+pagedata.post.type);
		},pagedata.paymodal.di_open)
	},
	pay_wechat:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_wechat(pagedata.paymodal.id,function(res){
				page.setData({
					showpaymodal:false,
				});
				app.util.msg(res.message,'','core/member.xunibi//type='+pagedata.post.type);
		},pagedata.paymodal.di_open);
	},
	pay_alipay:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_alipay(pagedata.paymodal.id,function(res){
				page.setData({
						showpaymodal:false,
				});
				app.util.msg(res.message,'','core/member.xunibi//type='+pagedata.post.type);
		},pagedata.paymodal.di_open);
	},
    pay_ttpay:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_ttpay(pagedata.paymodal.id,function(res){
				page.setData({
					showpaymodal:false,
				});
				app.util.msg(res.message,'','core/member.xunibi//type='+pagedata.post.type);
		},pagedata.paymodal.di_open)
	},
	pay_hdfk:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_hdfk(pagedata.paymodal.id,function(res){
				page.setData({
					showpaymodal:false,
				});
				app.util.msg(res.message,'','core/member.xunibi//type='+pagedata.post.type);
		})
	},
	pay_apipay:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_apipay(pagedata.paymodal.id,function(res){
				page.setData({
					showpaymodal:false,
				});
				app.util.msg(res.message,'','core/member.xunibi//type='+pagedata.post.type);
		},pagedata.paymodal.di_open,pagedata.paymodal.api_name)
	},
	radiotype:function(e){
		var the=this;
		var value=e.detail.value;
		var data=e.currentTarget.dataset;
		var pagedata=the.data.pagedata
		the.setData({
			'pagedata.paymodal.paytype':data.type,
			'pagedata.paymodal.api_name':data.name
		})
	},
	radiodi_open:function(e){
		var the=this;
		var value=e.detail.value;
		var data=e.currentTarget.dataset;
		var pagedata=the.data.pagedata
		var di_open=pagedata.paymodal.di_open;
		di_open=di_open==data.type?'':data.type;
		if(!di_open){
			data.difee=0;
		}
		the.setData({
			'pagedata.paymodal.di_open':di_open,
			'pagedata.paymodal.di_fee':data.difee
		})
	},
	new_gotopay:function(e){
		var the=this;
		var pagedata=the.data.pagedata
		var paytype=pagedata.paymodal.paytype;
		if(paytype=='credit'){
			the.pay_credit();
		}else if(paytype=='wechat'){
			the.pay_wechat();
		}else if(paytype=='alipay'){
			the.pay_alipay();
		}else if(paytype=='hdfk'){
			the.pay_hdfk();
		}else if(paytype=='apipay'){
			the.pay_apipay();
		}else if(paytype=='ttpay'){
			the.pay_ttpay();
		}else{
			app.util.msg('请选择支付方式！');
		}
	},
	formSubmit:function(e){
		var the = this
		var data=e.detail.value;
		var pagedata = the.data.pagedata;
		app.util.post('core/member.xunibi/recharge/',pagedata.post,function(pay_id){
			app.util.pay(pay_id,function(res){		
				if(res.type=='more'){
					the.setData({
						showpaymodal:true,
						'pagedata.paymodal':res
					});
				}else if(res.type=='success'){
					app.util.msg(res.message,'','core/member.xunibi//type='+pagedata.post.type);
				}else{
					app.util.msg(res.message);
				}
			});
		});
	},
	zhuanfee:function(e){
		var the =this
		var pagedata=the.data.pagedata
		var data=e.detail.value;
		pagedata.post.nopassword=1
		pagedata.post.fee=data.fee
		pagedata.post.mobile=data.mobile
		app.util.post('core/member.xunibi/zhuan/',pagedata.post);
	},
})